/*
 * Copyright 2010-2016 ksyun.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 *  http://ksyun.com/apache2.0
 *
 * or in the "license" file accompanying this file. This file is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */
package com.kingsoft.miot.bridge.iam.common;


import java.io.InputStream;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;


/**
 * Default implementation of the interface.
 * <p>
 * This class is only intended for internal use inside the KSC client libraries.
 * Callers shouldn't ever interact directly with objects of this class.
 */
public class DefaultRequest<T> implements SignableRequest<T>{

    /**
     * Map of the parameters being sent as part of this request.
     * <p>
     * Note that a LinkedHashMap is used, since we want to preserve the
     * insertion order so that members of a list parameter will still be ordered
     * by their indices when they are marshalled into the query string.
     *
     * Lists values in this Map must use an implementation that allows
     * null values to be present.
     */
    private Map<String, String> parameters = new HashMap<>();

    /** Map of the headers included in this request */
    private Map<String, String> headers = new HashMap<>();

    /** The service endpoint to which this request should be sent */
    private URI uri;


    /** The HTTP method to use when sending this request. */
    private String httpMethod = "POST";

    /** An optional stream from which to read the request payload. */
    private InputStream content;


    /**
     * Constructs a new DefaultRequest
     */
    public DefaultRequest() {
    }

    public void addHeader(String name, String value) {
        headers.put(name, value);
    }

    public Map<String, String> getHeaders() {
        return headers;
    }

    public void addParameter(String name, String value) {
        parameters.put(name, value);
    }

    public Map<String, String> getParameters() {
        return parameters;
    }

    public DefaultRequest<T> withParameter(String name, String value) {
        addParameter(name, value);
        return this;
    }

    public String getHttpMethod() {
        return httpMethod;
    }


    public void setHttpMethod(String httpMethod) {
        this.httpMethod = httpMethod;
    }

    public void setUri(URI uri) {
        this.uri = uri;
    }

    public URI getUri() {
        return uri;
    }

    public InputStream getContent() {
        return content;
    }

    public void setContent(InputStream content) {
        this.content = content;
    }

    public void setHeaders(Map<String, String> headers) {
        this.headers.clear();
        this.headers.putAll(headers);
    }

    public void setParameters(Map<String, String> parameters) {
        this.parameters.clear();
        this.parameters.putAll(parameters);
    }
}
